package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserMarriageData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMarriageDaoImpl extends TempDataObjectDao<UserMarriageData> {

    public Map<Long, UserMarriageData> getUserMarriageDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserMarriageData> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_marriage_detail";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMarriageData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public UserMarriageData getUserMarriageData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_marriage_detail where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMarriageData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public boolean addOfflineMarriageTimes(long userId) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_marriage_detail SET offlineMarriageTimes=offlineMarriageTimes+1 where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, userId);
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean addData(UserMarriageData data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_marriage_detail(userId,isAcceptAppoint,serverApplyRefreshTimes,buyServerApplyRefreshTimes,offlineMarriageTimes," +
                "modifyTime,totalMarriageTimes,lastMarriageApplyNotifyTime,totalLonelyCount,maxChildEarnSpeed,crossServerApplyRefreshTimes," +
                "buyCrossServerApplyRefreshTimes,isAcceptCrossAppoint) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setBoolean(i++, data.isAcceptAppoint());
            call.setInt(i++, data.getServerApplyRefreshTimes());
            call.setInt(i++, data.getBuyServerApplyRefreshTimes());
            call.setInt(i++, data.getOfflineMarriageTimes());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getTotalMarriageTimes());
            call.setLong(i++, data.getLastMarriageApplyNotifyTime());
            call.setLong(i++, data.getTotalLonelyCount());
            call.setString(i++, data.getMaxChildEarnSpeed().toString());
            call.setInt(i++, data.getCrossServerApplyRefreshTimes());
            call.setInt(i++, data.getBuyCrossServerApplyRefreshTimes());
            call.setBoolean(i++, data.isAcceptCrossAppoint());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserMarriageData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_marriage_detail SET isAcceptAppoint=?,serverApplyRefreshTimes=?,buyServerApplyRefreshTimes=?,offlineMarriageTimes=?,"
                + "modifyTime=?,totalMarriageTimes=?,lastMarriageApplyNotifyTime=?,totalLonelyCount=?,maxChildEarnSpeed=?,crossServerApplyRefreshTimes=?,"
                + "buyCrossServerApplyRefreshTimes=?,isAcceptCrossAppoint=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setBoolean(i++, data.isAcceptAppoint());
            call.setInt(i++, data.getServerApplyRefreshTimes());
            call.setInt(i++, data.getBuyServerApplyRefreshTimes());
            call.setInt(i++, data.getOfflineMarriageTimes());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getTotalMarriageTimes());
            call.setLong(i++, data.getLastMarriageApplyNotifyTime());
            call.setLong(i++, data.getTotalLonelyCount());
            call.setString(i++, data.getMaxChildEarnSpeed().toString());
            call.setInt(i++, data.getCrossServerApplyRefreshTimes());
            call.setInt(i++, data.getBuyCrossServerApplyRefreshTimes());
            call.setBoolean(i++, data.isAcceptCrossAppoint());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserMarriageData parseData(ResultSet rs) throws SQLException {
        UserMarriageData data = new UserMarriageData();
        data.setUserId(rs.getLong("userId"));
        data.setAcceptAppoint(rs.getBoolean("isAcceptAppoint"));
        data.setServerApplyRefreshTimes(rs.getInt("serverApplyRefreshTimes"));
        data.setBuyServerApplyRefreshTimes(rs.getInt("buyServerApplyRefreshTimes"));
        data.setOfflineMarriageTimes(rs.getInt("offlineMarriageTimes"));
        data.setTotalMarriageTimes(rs.getLong("totalMarriageTimes"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setLastMarriageApplyNotifyTime(rs.getLong("lastMarriageApplyNotifyTime"));
        data.setTotalLonelyCount(rs.getLong("totalLonelyCount"));
        String value = rs.getString("maxChildEarnSpeed");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setMaxChildEarnSpeed(new BigInteger(value));
        }
        data.setCrossServerApplyRefreshTimes(rs.getInt("crossServerApplyRefreshTimes"));
        data.setBuyCrossServerApplyRefreshTimes(rs.getInt("buyCrossServerApplyRefreshTimes"));
        data.setAcceptCrossAppoint(rs.getBoolean("isAcceptCrossAppoint"));
        return data;
    }
}
